package org.swimv2.servlet;

import java.io.IOException;
import java.util.Collection;
import java.util.Properties;
import java.util.Vector;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.swimv2.controller.GestioneNotifiche;
import org.swimv2.controller.GestioneNotificheRemote;
import org.swimv2.model.Persona;
import org.swimv2.util.Notifica;
import org.swimv2.util.Stato;
import org.swimv2.util.Tipo;



public class NotificheServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
     * @see HttpServlet#HttpServlet()
     */
    public NotificheServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        
        Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
		props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		Context ctx;
		try {
			/*ContextUtil o simile,funziona come l'omonimo dell'example3*/
			// vedere bene cosa fa lookup!!!!
			//Object obj = ContextUtil.getInitialContext().lookup("ManagerUtenti/remote");
			//GestioneNotificheRemote manager = (GestioneNotificheRemote) PortableRemoteObject.narrow(obj, GestioneNotificheRemote.class);
			
			ctx = new InitialContext(props);
			Object obj = ctx.lookup(GestioneNotifiche.JNDI_NAME);
			GestioneNotificheRemote manager = (GestioneNotificheRemote)PortableRemoteObject.narrow(obj,GestioneNotificheRemote.class);
			Persona u=(Persona)request.getSession().getAttribute("utente");
			String userID=u.getUserID();
			Collection <Notifica> notifiche=manager.getNotifiche(userID);
			Collection <Notifica> richiesteAmic,partecipazLav,esitiRichieste;
			richiesteAmic=new Vector<Notifica>();
			partecipazLav=new Vector<Notifica>();
			esitiRichieste=new Vector<Notifica>();
			
			for(Notifica n: notifiche){
				
				if(n.getStato() == Stato.PENDENTE){
					
					if(n.getTipo() == Tipo.AMICIZIA ){ richiesteAmic.add(n);   }	
					
					if(n.getTipo() == Tipo.LAVORO) { partecipazLav.add(n); }
				}
					
				else{
					esitiRichieste.add(n);
				}
			}
			request.setAttribute("notificAmicizie", richiesteAmic);
			request.setAttribute("notificLavori", partecipazLav);
			request.setAttribute("esitiRichieste", esitiRichieste);
			 RequestDispatcher disp = request.getRequestDispatcher("WEB-INF/notifiche.jsp");
			disp.forward(request, response);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}
	
}
